package com.example.weather.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.weather.Adapter.CitymanagerAdapter;
import com.example.weather.DB.DataBaseBean;
import com.example.weather.DB.Dbmanager;
import com.example.weather.R;

import java.util.ArrayList;
import java.util.List;

public class CityManagerActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView city_iv_back,city_iv_delete,city_iv_add;
    private ListView city_lv;
    private List<DataBaseBean> mdata;//列表数据源
    private CitymanagerAdapter citymanagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_manager);
        city_iv_add=findViewById(R.id.city_iv_add);
        city_iv_delete=findViewById(R.id.city_iv_delete);
        city_iv_back=findViewById(R.id.city_iv_back);
        city_lv=findViewById(R.id.city_lv);
        mdata=new ArrayList<>();
        city_iv_add.setOnClickListener(this);
        city_iv_delete.setOnClickListener(this);
        city_iv_back.setOnClickListener(this);
        //设置listview adapter
        citymanagerAdapter= new CitymanagerAdapter(this, mdata);
        city_lv.setAdapter(citymanagerAdapter);
    }
//获取焦点生命周期：获取数据库中真实数据源，添加到原有数据源中，提示适配器更新
    @Override
    protected void onResume() {
        super.onResume();
        //获取全部信息
        List<DataBaseBean> list = Dbmanager.queryall();
        mdata.clear();
        mdata.addAll(list);
        citymanagerAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.city_iv_add:
                int citycount= Dbmanager.getcitycount();
                if (citycount<5){
                startActivity(new Intent(CityManagerActivity.this,AddcityActivity.class));}
                else {
                    Toast.makeText(this,"存储城市数量已达上限，请删除后再增加",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.city_iv_back:
                finish();
                break;
            case R.id.city_iv_delete:
                startActivity(new Intent(CityManagerActivity.this,DeleteCityActivity.class));
                break;
        }
    }
}
